﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YourWallet
{
    public partial class Form1 : Form
    {
        AccountsManagement accountsMag;

        public Form1()
        {
            InitializeComponent();
            accountsMag = new AccountsManagement();
            accountsMag.AddAccount("Main");
            labSelectedDate.Text = convertMonthToString(DateTime.Today.Month);
            radioMonthly.Checked = true;

            XMLManagement xmlMag = new XMLManagement();
            accountsMag.AddTransactions(xmlMag.ReadXML());
            printInfo();
        }

        #region Info

        private void printInfo()
        {
            labIncome.Text = accountsMag.GetIncome().ToString() + " zl";
            labExpense.Text = accountsMag.GetExpense().ToString() + " zl";
            labBalance.Text = accountsMag.GetBalance().ToString() + " zl";
        }

        private void butExpense_Click(object sender, EventArgs e)
        {
            radioNewExpense.Checked = true;
            if (!prepareNewTransaction())
            {
                return;
            }
            showNewTransactionGroup();
        }

        private void butIncome_Click(object sender, EventArgs e)
        {
            radioNewIncome.Checked = true;
            if (!prepareNewTransaction())
            {
                return;
            }
            showNewTransactionGroup();
        }

        #endregion

        #region Date

        private void ptintDate()
        {
            if (accountsMag.IsActiveMonth())
            {
                string year = "";
                if (accountsMag.GetActiveDate().Year != DateTime.Today.Year)
                {
                    year = " " + accountsMag.GetActiveDate().Year.ToString();
                }
                labSelectedDate.Text = convertMonthToString(accountsMag.GetActiveDate().Month) + year;
            }
            else if (accountsMag.IsActiveYear())
            {
                labSelectedDate.Text = accountsMag.GetActiveDate().Year.ToString();
            }
        }

        private void radioMonthly_CheckedChanged(object sender, EventArgs e)
        {
            accountsMag.SetActiveMonth();
            ptintDate();
            printInfo();
            printTransactions();
        }

        private void radioYearly_CheckedChanged(object sender, EventArgs e)
        {
            accountsMag.SetActiveYear();
            ptintDate();
            printInfo();
            printTransactions();
        }

        private void butLeft_Click(object sender, EventArgs e)
        {
            accountsMag.DecreaseDate();
            ptintDate();
            printInfo();
            printTransactions();
        }

        private void butRight_Click(object sender, EventArgs e)
        {
            accountsMag.IncreaseDate();
            ptintDate();
            printInfo();
            printTransactions();
        }

        private string convertMonthToString(int month)
        {
            string result = "";
            switch (month)
            {
                case 1: result = "Styczen"; break;
                case 2: result = "Luty"; break;
                case 3: result = "Marzec"; break;
                case 4: result = "Kwiecien"; break;
                case 5: result = "Maj"; break;
                case 6: result = "Czerwiec"; break;
                case 7: result = "Lipiec"; break;
                case 8: result = "Sierpien"; break;
                case 9: result = "Wrzesien"; break;
                case 10: result = "Pazdziernik"; break;
                case 11: result = "Listopad"; break;
                case 12: result = "Grudzien"; break;
            }
            return result;
        }

        #endregion

        #region NewTransaction

        private bool prepareNewTransaction()
        {
            cleanGroupNewTrans();
            string activeAccount = accountsMag.GetActiveAccount();
            if ("" == activeAccount)
            {
                return false;
            }

            foreach (string account in accountsMag.GetAccountsNames())
            {
                comboNewAccount.Items.Add(account);
            }
            comboNewAccount.Text = activeAccount;
            dateNewTimePicker1.Value = DateTime.Today;
            return true;
        }

        private void butNewCancel_Click(object sender, EventArgs e)
        {            
            showInfoGroup();
            printInfo();
        }

        private void butNewDone_Click(object sender, EventArgs e)
        {
            if (!addNewTransaction()) return;
            showInfoGroup();
            printInfo();
        }

        private bool addNewTransaction()
        {
            DateTime date = new DateTime(dateNewTimePicker1.Value.Year,
                dateNewTimePicker1.Value.Month, dateNewTimePicker1.Value.Day);

            double amount;
            if (!Double.TryParse(txtNewAmount.Text, out amount)) //amount
            {
                MessageBox.Show("Wrong amount!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            string category = txtNewCategory.Text;
            if ("" == category)
            {
                MessageBox.Show("Category must be entered", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (!accountsMag.SetActiveAccount(comboNewAccount.Text))
            {
                MessageBox.Show("Account doesn't exist!", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            string note = txtNewNote.Text;

            if (radioNewExpense.Checked == true)
            {
                accountsMag.AddExpense(amount, category, date, note);
            }
            else if (radioNewIncome.Checked == true)
            {
                accountsMag.AddIncome(amount, category, date, note);
            }

            return true;
        }

        private void cleanGroupNewTrans()
        {
            txtNewAmount.Text = "";
            txtNewCategory.Text = "";
            txtNewNote.Text = "";
        }

        #endregion

        #region Transactions

        private void printTransactions()
        {
            listTransactions.Items.Clear();
            for (int i = 0; i < accountsMag.GetNumberOfTransactions(); ++i)
            {
                ListViewItem item1 = new ListViewItem(accountsMag.GetTransactionName(i), i);
                item1.SubItems.Add(accountsMag.GetTransactionAmount(i).ToString());
                listTransactions.Items.AddRange(new ListViewItem[] { item1 });
                if (accountsMag.IsExpense(i))
                {
                    listTransactions.Items[i].ForeColor = Color.Red;
                }
                else if (accountsMag.IsIncome(i))
                {
                    listTransactions.Items[i].ForeColor = Color.Green;
                }
            }
        }

        #endregion

        #region Transaction

        private void prepareTransaction()
        {
            string activeAccount = accountsMag.GetActiveAccount();
            foreach (string account in accountsMag.GetAccountsNames())
            {
                comboAccount.Items.Add(account);
            }
            comboAccount.Text = activeAccount;

            int index = getSelectedIndex();
            dateTimePicker2.Value = accountsMag.GetTransactionDate(index);
            txtAmount.Text = accountsMag.GetTransactionAmount(index).ToString();
            txtCategory.Text = accountsMag.GetTransactionCategory(index);
            txtNote.Text = accountsMag.GetTransactionNote(index);
            radioExpense.Checked = accountsMag.IsExpense(index);
            radioIncome.Checked = accountsMag.IsIncome(index);
        }

        private void listTransactions_SelectedIndexChanged(object sender, EventArgs e)
        {
            prepareTransaction();
            showTransactionGroup();
        }

        private int getSelectedIndex()
        {
            for (int i = 0; i < listTransactions.Items.Count; ++i)
            {
                if (listTransactions.Items[i].Selected == true)
                {
                    return i;
                }
            }
            return -1;
        }

        private bool ChangeTransaction()
        {
            DateTime date = new DateTime(dateTimePicker2.Value.Year,
                dateTimePicker2.Value.Month, dateTimePicker2.Value.Day);

            double amount;
            if (!Double.TryParse(txtAmount.Text, out amount))
            {
                MessageBox.Show("Wrong amount!", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            string category = txtCategory.Text;
            if ("" == category)
            {
                MessageBox.Show("Category must be entered!", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }            
            
            string account = comboAccount.Text;
            if (!accountsMag.GetAccountsNames().Contains(account))
            {
                MessageBox.Show("Account doesn't exist!", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            string note = txtNote.Text;

            if (radioExpense.Checked == true)
            {
                accountsMag.SetTransactionParameters(getSelectedIndex(), account, amount, category,
                    date, note, TransactionType.Expense);
            }
            else if (radioIncome.Checked == true)
            {
                accountsMag.SetTransactionParameters(getSelectedIndex(), account, amount, category,
                    date, note, TransactionType.Income);
            }

            return true;
        }

        private void butDone_Click(object sender, EventArgs e)
        {
            if (!ChangeTransaction())
            {
                return;
            }

            showTransactionsGroup();
            printTransactions();
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            showTransactionsGroup();
            printTransactions();
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            accountsMag.DeleteTransaction(getSelectedIndex());
            showTransactionsGroup();
            printTransactions();
        }

        #endregion

        #region Menu

        private void butSpending_Click(object sender, EventArgs e)
        {
            showInfoGroup();
            printInfo();
        }

        private void butTransactions_Click(object sender, EventArgs e)
        {
            showTransactionsGroup();
            printTransactions();
        }

        private void showTransactionGroup()
        {
            groupDate.Visible = false;
            groupInfo.Visible = false;
            groupTransactions.Visible = false;
            groupNewTrans.Visible = false;
            groupTrans.Visible = true;
        }

        private void showNewTransactionGroup()
        {
            groupDate.Visible = false;
            groupInfo.Visible = false;
            groupTransactions.Visible = false;
            groupNewTrans.Visible = true;
            groupTrans.Visible = false;
        }

        private void showTransactionsGroup()
        {
            groupDate.Visible = true;
            groupInfo.Visible = false;
            groupTransactions.Visible = true;
            groupNewTrans.Visible = false;
            groupTrans.Visible = false;
        }

        private void showInfoGroup()
        {
            groupDate.Visible = true;
            groupInfo.Visible = true;
            groupTransactions.Visible = false;
            groupNewTrans.Visible = false;
            groupTrans.Visible = false;
        }
        #endregion

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            XMLManagement xmlMag = new XMLManagement();
            xmlMag.WriteXML(accountsMag.GetAllTransations());
        }
    }
}
